#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QTextCodec>
#include <QDebug>
#include "../utils/cJSON.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    timer(new QTimer(this))
{
    // 字体颜色
    QString fontColor = "color:#ffffff;";
    QString backgroundColor = "background-color: rgb(77,158,212)";
    QString welcomeString = ""; //"欢迎光临(调试中)";

    #if 0 //屏蔽切换
    connect(timer,SIGNAL(timeout()), this, SLOT(onTimeOut()));
    timer->start(1500);
    #endif

    ui->setupUi(this);
    setStyleSheet(backgroundColor);
    QFont font;
    font.setPointSize(60);
    ui->label->setFont(font);
    ui->label->setStyleSheet(fontColor);
    ui->label->setText(welcomeString);

    //ui->label_2->setStyleSheet(fontColor);
    //ui->label_2->setFont(font);
    //ui->label_2->hide();
    //ui->textEdit->insertPlainText("hello1");
}

MainWindow::~MainWindow()
{
    delete timer;
    delete ui;
}

int MainWindow::init_signal()
{
    connect(this, SIGNAL(do_signal(char*)), this, SLOT(onMySignal(char *)),
            Qt::BlockingQueuedConnection);
    return 0;
}

int MainWindow::signal(char *arg)
{
    emit do_signal(arg);
    return 0;
}

int MainWindow::onMySignal(char *arg)
{
    switch (arg[0])
    {
        case '0':
            ui->textEdit->insertPlainText(arg+1);
            ui->textEdit->ensureCursorVisible();
            break;

        case '1':
        {
            QTextCodec* utf8Codec= QTextCodec::codecForName("utf-8");
            QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");
            QString strUnicode= gb2312Codec->toUnicode(arg+1);
            QByteArray byteUtf8= utf8Codec->fromUnicode(strUnicode);
            ui->textEdit->insertPlainText(byteUtf8.data());
            ui->textEdit->ensureCursorVisible();
            break;
        }
    }

    return 0;
}

void MainWindow::keyPressEvent(QKeyEvent *ev)
{
    if(ev->key()==Qt::Key_Escape)
    {
        showNormal();
    }
    else if(ev->key()==Qt::Key_A)
    {
        switchWelcome();
    }
    else if(ev->key()==Qt::Key_B)
    {
        switchFull();
    }
    else if(ev->key()==Qt::Key_C)
    {
        switchFree();
    }
    else if(ev->key()==Qt::Key_D)
    {
        switchCar();
    }
}

void MainWindow::onTimeOut()
{
    typedef void (MainWindow::*MemberPtr)();
    MemberPtr array[] = {
        &MainWindow::switchWelcome,
        &MainWindow::switchFull,
        &MainWindow::switchFree,
        &MainWindow::switchCar
    };
    static int count = 0;
    int size = sizeof(array)/sizeof(MemberPtr);
    //std::cout << "size: " << size << std::endl;
    count++;
    (this->*array[count%size])();

    switch(count%4)
    {
    case 1:
        displayer.process_event(Displayer::EventCome());
        break;
    case 2:
        displayer.process_event(Displayer::EventIn());
        break;
    case 3:
        displayer.process_event(Displayer::EventCheck());
        break;
    default:
        displayer.process_event(Displayer::EventLeave());
        break;
    }

    qDebug() << "state: " << displayer.current_state();
}

void MainWindow::switchWelcome()
{
    //ui->label_2->hide();
}

void MainWindow::switchFull()
{
    QString s = "车位已满";
    //ui->label_2->setText(s.split("", QString::SkipEmptyParts).join("\n"));
    //ui->label_2->show();
}

void MainWindow::switchFree()
{
    //QString s = "剩余车位\n100";
    //ui->label_2->setText(s);
    //ui->label_2->show();
}

void MainWindow::switchCar()
{
    QString s = "临时车\n粤A 88888";
    //ui->label_2->setText(s);
    //ui->label_2->show();
}
